local AddOnName, Env = ... local ADDON = Env[1]
ADDON.Log = {} 
local Log = ADDON.Log
-- ~~| Libraries |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local DiesalTools 	= LibStub("DiesalTools-1.0")
local DiesalStyle 	= LibStub("DiesalStyle-1.0") 
local DiesalGUI 		= LibStub("DiesalGUI-1.0")
-- ~~| Lua Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local format, rep, len																	= string.format, string.rep, string.len
local concat, remove																		= table.concat, table.remove
-- ~~| Development Log |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~| Development Log Locals |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local initialized			= false
local	sessionMessages 	= {}	
local sessionHistory
local databaseMessages
local	databaseHistory	= 3000	

local function Log2Database()	
	local n		= #databaseMessages
	local lastMessage = databaseMessages[n]	
		
	if lastMessage and lastMessage.msg == msg then				
		lastMessage.count = lastMessage.count + 1		
	else					
		databaseMessages[n + 1] = {msg = msg, count = 1}
	end			
	
	if n >= databaseHistory then remove(databaseMessages,1) end		
end
-- ~~| Development Log API |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function Log:AddMessage(msg)	
	if not msg or initialized and not ADDON.db.profile.developer.logging then return end
	local n = #sessionMessages	
	local lastMessage = sessionMessages[n]
		
	if lastMessage and lastMessage.msg == msg then				
		lastMessage.count = lastMessage.count + 1				
	else					
		sessionMessages[n + 1] = {msg = msg, count = 1}
	end			
	-- initialized	
	if initialized then 	
		if n >= sessionHistory then remove(sessionMessages,1) end
		Log2Database(msg)
		self:Output()					
	end	
end
function Log:Initialize()		
	DiesalDevelopmentDB.log = DiesalDevelopmentDB.log or {}			
	databaseMessages 			= DiesalDevelopmentDB.log
	-- insert messages created before database initialized
	for i = 1, #sessionMessages do databaseMessages[#databaseMessages + 1] = sessionMessages[i] end
	
	self:SetMax(ADDON.db.profile.developer.sessionHistory)			
	initialized = true
end
function Log:SetMax(num)		
	sessionHistory = num			
	for i=1, #sessionMessages - num do remove(sessionMessages,1)	end
	self:Output()	
end
function Log:Output()		
	if not next(sessionMessages) then ADDON.Developer:SetText('') return end
	local text
	local messageCount = sessionMessages[1].count				
	
	text = format('|cff%s%s%s |r%s', messageCount == 1 and '444444' or '999999', rep('0',3 - len(messageCount)), messageCount, sessionMessages[1].msg)							
	
	for i = 2, #sessionMessages do
		messageCount = sessionMessages[i].count				
		text = format('%s\n|cff%s%s%s |r%s',text, messageCount == 1 and '444444' or '999999', rep('0',3 - len(messageCount)), messageCount, sessionMessages[i].msg)				
	end			
	
	ADDON.Developer:SetText(text)
end
function Log:Clear()	
	for i = 1, #sessionMessages do
		sessionMessages[i] = nil	
	end
	self:Output()	
end	




